//////////////////////////////////////////////////////////////////////
//
//  Class Name: MySQLInfo
//
//  Description: This Class Defines the basic information needed to 
//               connect to the MySQL Database.
//
//  Revision History
//  Christian Romvary  16JUN2013
//
//////////////////////////////////////////////////////////////////////

package edu.psu.collegerecommendationhelper.coreservlets;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;

import java.util.ArrayList;
import java.util.List;



import edu.psu.collegerecommendationhelper.algorithm.GenerateRecommendations;
import edu.psu.collegerecommendationhelper.algorithm.RecommendationEngine;
import edu.psu.collegerecommendationhelper.database.GenericDatabase;
import edu.psu.collegerecommendationhelper.database.MySQLDatabase;
import edu.psu.collegerecommendationhelper.model.CollegeResult;
import edu.psu.collegerecommendationhelper.model.DemographicInfo;
import edu.psu.collegerecommendationhelper.model.EducationInfo;
import edu.psu.collegerecommendationhelper.model.RecommendationListing;
import edu.psu.collegerecommendationhelper.model.UserBean;

@ManagedBean(name="modifyResult")
public class CollegeResultBean {
@ManagedProperty(value="#{userBean}")
   private UserBean user;

   private byte entered_active;

   private String entered_result1;
   private String entered_result2;
   private String entered_result3;
   private String entered_result4;
   private String entered_result5;

   private CollegeResult resultList;


   public UserBean getUser() {
      return user;
   }
   public void setUser(UserBean user) {
      this.user = user;
   }

   public byte getEntered_active() {
      return entered_active;
   }
   public void setEntered_active(byte entered_active) {
      this.entered_active = entered_active;
   }

   public String getEntered_result1() {
      return entered_result1;
   }
   public void setEntered_result1(String entered_result1) {
      this.entered_result1 = entered_result1;
   }

   public String getEntered_result2() {
      return entered_result2;
   }
   public void setEntered_result2(String entered_result2) {
      this.entered_result2 = entered_result2;
   }

   public String getEntered_result3() {
      return entered_result3;
   }
   public void setEntered_result3(String entered_result3) {
      this.entered_result3 = entered_result3;
   }

   public String getEntered_result4() {
      return entered_result4;
   }
   public void setEntered_result4(String entered_result4) {
      this.entered_result4 = entered_result4;
   }

   public String getEntered_result5() {
      return entered_result5;
   }
   public void setEntered_result5(String entered_result5) {
      this.entered_result5 = entered_result5;
   }

   public CollegeResult getResultList() {
      return resultList;
   }
   public void setResultList(CollegeResult resultList) {
      this.resultList = resultList;
   }


////////////////////////////////////////////////////////////////////////

   public String saveResultList() {
      MySQLDatabase mysqlDB = new MySQLDatabase();
      GenericDatabase genDB;
      genDB = mysqlDB;

      boolean action = false;
      resultList = user.getResultList();

      if ((!entered_result1.equals(resultList.getResult1())) &&
          (!entered_result1.isEmpty())) {
         resultList.setResult1(entered_result1);
         action = true;
      }

      if ((!entered_result2.equals(resultList.getResult2())) &&
          (!entered_result2.isEmpty())) {
         resultList.setResult2(entered_result2);
         action = true;
      }

      if ((!entered_result3.equals(resultList.getResult3())) &&
          (!entered_result3.isEmpty())) {
         resultList.setResult3(entered_result3);
         action = true;
      }

      if ((!entered_result4.equals(resultList.getResult4())) &&
          (!entered_result4.isEmpty())) {
         resultList.setResult4(entered_result4);
         action = true;
      }

      if ((!entered_result5.equals(resultList.getResult5())) &&
          (!entered_result5.isEmpty())) {
         resultList.setResult5(entered_result5);
         action = true;
      }


      if(true == action) {
         user.setResultList(resultList);
         genDB.saveResultList(user.getUser_id(), resultList);

         return ("MainPage");
      }
      else {
         return null;
      }
   }

////////////////////////////////////////////////////////////////////////

   public String generateResultList() {
      MySQLDatabase mysqlDB = new MySQLDatabase();
      GenericDatabase genDB;
      genDB = mysqlDB;

//System.out.println("Attempting to initialize RecommendationEngine");
//      RecommendationEngine recommender = new RecommendationEngine();
//System.out.println("Declaring GenerateRecommendations variable");
//      GenerateRecommendations genRec;
//      genRec = recommender;

      RecommendationListing collegeList;

      List<RecommendationListing> generateList = new ArrayList<RecommendationListing>();
      
      CollegeResult result = user.getResultList();

      boolean action = false;

// Actual Generate Function
//      generateList = genRec.createListing(user.getDemographicInfo(), user.getEducationInfo(), user.getPreferenceInfo());

// Test Generate Function
      generateList = testGenerateList();

      //Initialize ResultList to Empty
      result.setCollege1("");
      result.setCollege2("");
      result.setCollege3("");
      result.setCollege4("");
      result.setCollege5("");

      int i = 0;
      //Analyze Results
      while ((5 != 0) && (!generateList.isEmpty())) {
         collegeList = generateList.get(0);
         generateList.remove(0);

         action =true;

         i = i+1;

         if (1 == i) { 
            result.setCollege1(collegeList.getName());
System.out.println("College 1: " + collegeList.getName());
         }
         else if (2 == i) {
            result.setCollege2(collegeList.getName());
System.out.println("College 2: " + collegeList.getName());
         }
         else if (3 == i) {
            result.setCollege3(collegeList.getName());
System.out.println("College 3: " + collegeList.getName());
         }
         else if (4 == i) {
            result.setCollege4(collegeList.getName());
System.out.println("College 4: " + collegeList.getName());
         }
         else if (5 == i) {
            result.setCollege5(collegeList.getName());
System.out.println("College 5: " + collegeList.getName());
         }
      }

      //If ResultList changed, Save
      if(true == action) {
         user.setResultList(result);
         genDB.saveResultList(user.getUser_id(), result);
      }

      return null;
   }

////////////////////////////////////////////////////////////////////////

   private List<RecommendationListing> testGenerateList() {
      List<RecommendationListing> results = new ArrayList<RecommendationListing>();

      RecommendationListing rl = new RecommendationListing();
      RecommendationListing rl1 = new RecommendationListing();
      RecommendationListing rl2 = new RecommendationListing();
      RecommendationListing rl3 = new RecommendationListing();
      RecommendationListing rl4 = new RecommendationListing();

      rl.setAddress("State College, PA");
      rl.setName("Penn State University");
      rl.setURL("www.psu.edu");
      results.add(0, rl);

      rl1.setAddress("Ann Arbor, MI");
      rl1.setName("University of Michigan");
      rl1.setURL("www.um.edu");
      results.add(1, rl1);

      rl2.setAddress("Philadelphia, PA");
      rl2.setName("Saint Josephs University");
      rl2.setURL("www.sju.edu");
      results.add(2, rl2);

      rl3.setAddress("Glenside, PA");
      rl3.setName("Arcadia University");
      rl3.setURL("www.arcadia.edu");
      results.add(3, rl3);

      rl4.setAddress("Wilkes Barre, PA");
      rl4.setName("Kings College");
      rl4.setURL("www.kings.edu");
      results.add(4, rl4);

      return results;

   }


}